﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Web.Old_App_Code;
using AjaxPro;

public partial class backstage_order : System.Web.UI.Page
{
    // Fields
    protected DeliveryWayManager dwm = new DeliveryWayManager();

    protected GoodsEntitysSpecificationsManager gesm = new GoodsEntitysSpecificationsManager();

    protected OrderGoodsEntitysManager ogem = new OrderGoodsEntitysManager();
    protected OrderManager om = new OrderManager();
    protected Order or = new Order();
    protected string ot = "";
    protected string registerIP = "";
    protected string registerTime = "";
    protected RegionManager rm = new RegionManager();


    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            if (base.Request.QueryString["id"] != null)
            {
                this.or.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                this.or.GoodsTotal = Convert.ToDecimal(this.lblGoodsTotal.InnerText.Replace("￥", "").Replace("元", ""));
                this.or.OrderTotal = Convert.ToDecimal(this.lblGoodsTotal.InnerText.Replace("￥", "").Replace("元", "")) + Convert.ToDecimal(this.txtDeliveryMoney.Value);
                this.or.DeliveryWay = base.Request.Form["deliveryWay"];
                this.or.Payment = this.selectPayment.Value;
                this.or.DeliveryMoney = Convert.ToDecimal(this.txtDeliveryMoney.Value);
                this.or.Postscript = this.spanPostscript.InnerHtml;
                this.or.PayStatus = Convert.ToInt32(this.selectPayStatus.Value);
                this.or.DeliveryStatus = Convert.ToInt32(this.selectDeliveryStatus.Value);
                this.or.OrderStatus = Convert.ToInt32(this.selectOrderStatus.Value);
                this.or.Consignee = this.txtConsignee.Value;
                DataTable regionById = this.rm.GetRegionById(Convert.ToInt32(base.Request.Form["provinces"]));
                this.or.Region = regionById.Rows[0]["regionName"].ToString();
                if (base.Request.Form["city"] != null)
                {
                    this.or.Region = this.or.Region + "," + base.Request.Form["city"];
                }
                this.or.Address = this.txtAddress.Value;
                this.or.ZipCode = this.txtZipCode.Value;
                this.or.Telephone = this.txtTelephone.Value;
                this.or.Mobile = this.txtMobile.Value;
                this.or.PresentedIntegra = Convert.ToInt32(this.txtPresentedIntegra.Value);
                if (this.om.modifyOrder(this.or))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"order_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectUserRank\",\"" + base.Request.Form["userRank"] + "\");alert(\"更新失败！\");</script>");
                }
            }
        }
        catch
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectUserRank\",\"" + base.Request.Form["userRank"] + "\");alert(\"更新失败！\");</script>");
        }
    }

    protected StringBuilder DeliveryWayBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable deliveryWayByLanguageVersion = this.dwm.GetDeliveryWayByLanguageVersion(this.Session["language_version"].ToString());
            DataTable orderById = this.om.GetOrderById(Convert.ToInt32(base.Request.QueryString["id"]));
            foreach (DataRow row in deliveryWayByLanguageVersion.Rows)
            {
                builder.AppendLine("<option value=\"" + row["deliveryWayName"].ToString() + "\" " + ((row["deliveryWayName"].ToString() == orderById.Rows[0]["deliveryWay"].ToString()) ? "selected='selected'" : "") + ">" + row["deliveryWayName"].ToString() + "</option>");
            }
            builder.AppendLine("<option value=\"卖家承担运费\" " + (("卖家承担运费" == orderById.Rows[0]["deliveryWay"].ToString()) ? "selected='selected'" : "") + ">卖家承担运费</option>");
            return builder;
        }
        catch
        {
            return builder;
        }
    }

    protected string GetGoodsEntitysSpecifications(object goodsEntitysId)
    {
        string str = "";
        try
        {
            DataTable table = this.gesm.FrontDeskGetGoodsEntitysSpecificationsByGoodsEntitys(Convert.ToInt32(goodsEntitysId));
            for (int i = 0; i < table.Rows.Count; i++)
            {
                object obj2 = str;
                str = string.Concat(new object[] { obj2, (i > 0) ? "&nbsp;&nbsp;" : "", "[", table.Rows[i]["specificationsName"], "：", table.Rows[i]["specificationsValueName"], "]" });
            }
        }
        catch
        {
        }
        return str;
    }

    private void OrderBind()
    {
        try
        {
            DataTable orderById = this.om.GetOrderById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (orderById.Rows.Count > 0)
            {
                DataRow row = orderById.Rows[0];
                this.lblOrderNo.InnerText = row["orderNo"].ToString();
                this.lblCreateTime.InnerText = row["createTime"].ToString();
                this.lblGoodsTotal.InnerText = "￥" + row["goodsTotal"].ToString() + "元";
                this.lblOrderTotal.InnerText = "￥" + row["orderTotal"].ToString() + "元";
                this.selectPayment.Value = row["payment"].ToString();
                this.txtDeliveryMoney.Value = row["deliveryMoney"].ToString();
                this.selectPayStatus.Value = row["payStatus"].ToString();
                this.spanPostscript.InnerHtml = row["postscript"].ToString();
                this.spOrderRemark.InnerHtml = row["orderRemark"].ToString();
                this.selectDeliveryStatus.Value = row["deliveryStatus"].ToString();
                this.selectOrderStatus.Value = row["orderStatus"].ToString();
                this.txtConsignee.Value = row["consignee"].ToString();
                this.txtAddress.Value = row["address"].ToString();
                this.txtZipCode.Value = row["zipCode"].ToString();
                this.txtTelephone.Value = row["telephone"].ToString();
                this.txtMobile.Value = row["mobile"].ToString();
                this.txtPresentedIntegra.Value = row["presentedIntegra"].ToString();
            }
        }
        catch
        {
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        Utility.RegisterTypeForAjax(typeof(Region_ajax));
        if (!base.IsPostBack && ((base.Request.QueryString["id"] != null) && (base.Request.QueryString["orderNo"] != null)))
        {
            this.OrderBind();
            this.rptOrderGoodsEntitysBind();
        }
    }

    protected StringBuilder PrintBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if ((base.Request.QueryString["id"] != null) && (base.Request.QueryString["orderNo"] != null))
            {
                builder.AppendLine("<input id=\"btnPrint\" type=\"button\" class=\"formButton\" value=\"打印订单\" onclick=\"javascript:window.open('order_report_forms.aspx?id=" + base.Request.QueryString["id"] + "&orderNo=" + base.Request.QueryString["orderNo"] + "');\" />");
            }
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder ProvincesBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            builder.AppendLine("<select id=\"selectProvinces\" name=\"provinces\">");
            builder.AppendLine("<option value=\"0\">请选择...</option>");
            DataTable regionByParentRegionAndLanguageVersion = this.rm.GetRegionByParentRegionAndLanguageVersion(0, this.Session["language_version"].ToString());
            DataTable orderById = this.om.GetOrderById(Convert.ToInt32(base.Request.QueryString["id"]));
            string str = (orderById.Rows[0]["region"].ToString().IndexOf(",") == -1) ? orderById.Rows[0]["region"].ToString() : orderById.Rows[0]["region"].ToString().Split(new char[] { ',' }).GetValue(0).ToString();
            string str2 = (orderById.Rows[0]["region"].ToString().IndexOf(",") == -1) ? orderById.Rows[0]["region"].ToString() : orderById.Rows[0]["region"].ToString().Split(new char[] { ',' }).GetValue(1).ToString();
            int parentRegion = 0;
            foreach (DataRow row in regionByParentRegionAndLanguageVersion.Rows)
            {
                if (str == row["regionName"].ToString())
                {
                    parentRegion = Convert.ToInt32(row["id"]);
                }
                builder.AppendLine(string.Concat(new object[] { "<option value=\"", row["id"], "\" ", (str == row["regionName"].ToString()) ? "selected='selected'" : "", ">", row["regionName"], "</option>" }));
            }
            builder.AppendLine("</select><span id=\"spanCity\" style=\"color: #000000;\">");
            DataTable table3 = this.rm.GetRegionByParentRegionAndLanguageVersion(parentRegion, this.Session["language_version"].ToString());
            if (table3.Rows.Count > 0)
            {
                builder.AppendLine("<select id=\"selectCity\" name=\"city\">");
                builder.AppendLine("<option value=\"0\">请选择...</option>");
                foreach (DataRow row in table3.Rows)
                {
                    builder.AppendLine(string.Concat(new object[] { "<option value=\"", row["regionName"], "\" ", (str2 == row["regionName"].ToString()) ? "selected='selected'" : "", ">", row["regionName"], "</option>" }));
                }
                builder.AppendLine("</select>");
            }
            builder.AppendLine("</span>");
        }
        catch
        {
        }
        return builder;
    }

    protected void rptOrderGoodsEntitys_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    }

    private void rptOrderGoodsEntitysBind()
    {
        this.rptOrderGoodsEntitys.DataSource = this.ogem.GetOrderGoodsEntitysByOrderNo(base.Request.QueryString["orderNo"]);
        this.rptOrderGoodsEntitys.DataBind();
    }

}